# -*- coding: utf-8 -*- 
from datetime import date
from datetime import timedelta
import random
from faker import Faker
 
def create_phone(): 
    # 第二位数字 
    second = [3, 4, 5, 7, 8][random.randint(0, 4)] 
    # 第三位数字 
    third = { 3: random.randint(0, 9), 
        4: [5, 7, 9][random.randint(0, 2)], 
        5: [i for i in range(10) if i != 4][random.randint(0, 8)], 
        7: [i for i in range(10) if i not in [4, 9]][random.randint(0, 7)], 
        8: random.randint(0, 9), }[second] 
    # 最后八位数字 
    suffix = random.randint(9999999,100000000) 
    # 拼接手机号 
    return "1{}{}{}".format(second, third, suffix) 
    # 生成手机号

 
def getdistrictcode():
    codelist = []
    file = open('districtcode.txt')
    lines = file.readlines() #逐行读取
    for line in lines:
        if  line.lstrip().rstrip().strip()!='' and (line.lstrip().rstrip().strip())[:6][-2:]!='00': #如果每行中去重后不为空，并且6位数字中最后两位不为00，则添加到列表里。（最后两位为00时为省份或地级市代码）
            codelist.append(line[:6])
            # print(line[:6])
            # print(codelist)
    return codelist
def gennerator(nian):
    codelist = getdistrictcode()
    id = codelist[random.randint(0,329)] #地区项'''len(codelist)'''
    id = id +nian# str(random.randint(1950,1998)) #年份项
    da  = date.today()+timedelta(days=random.randint(1,366)) #月份和日期项
    id = id + da.strftime('%m%d')
    id = id+ str(random.randint(100,300))#，顺序号简单处理
 
    i = 0
    count = 0
    weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] #权重项
    checkcode ={'0':'1','1':'0','2':'X','3':'9','4':'8','5':'7','6':'6','7':'5','8':'5','9':'3','10':'2'} #校验码映射
    for i in range(0,len(id)):
       count = count +int(id[i])*weight[i]
    id = id + checkcode[str(count%11)] #算出校验码
    return id

if __name__ == '__main__':
    def getCustName():
        name = Faker('zh_CN').name()
        return name
    def sexPd(id):
        sex = id[16:17]
        print(sex)
        if int(sex) % 2==0:
            return 1
        else:
            return 0
    def Birthday(id):
        birthday = id[6:14]
        return birthday
    for k in range(0,1):
        nian=1975+k
        file = open("C:/TEST/sfz"+str(nian)+".txt",'a')
        file.write('P_prtNo,P_appntID,P_appntPhone,P_appntName,P_appntSex,P_birthday'+'\n')
        for i in range(231530,20000):
            prtNo = '20190103'+str(i).zfill(7) #231529
            id=gennerator(str(nian))
            P_sex =sexPd(id)
            P_birthday =Birthday(id)
            print(str(prtNo)+','+str(id)+','+create_phone()+','+getCustName()+','+str(P_sex)+','+str(P_birthday))
            file.write(str(prtNo)+','+str(id)+','+create_phone()+','+getCustName()+','+str(P_sex)+','+str(P_birthday)+'\n')
    file.close()

    print("生产完成！")
